package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.SunriseSunsetTimeBean;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

public class BottomSunriseSunsetDialogNew extends BottomPopupView {
    private BottomSunriseSunsetDialogNew.DialogClickListener mDialogClickListener;
    private TextView tvLabel;
    private WheelView wv_first;
    private WheelView wv_second;
    private ArrayWheelAdapter mFirstNumericWheelAdapter;
    private ImageView wheelImageView;
    private ArrayWheelAdapter mSecondNumericWheelAdapter;
    private int startH;
    private int startM;
    private boolean isSingle;

    public BottomSunriseSunsetDialogNew(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_sunrise_sunset_new;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        tvLabel = findViewById(R.id.tv_label);
        wv_first = findViewById(R.id.options1);
        wheelImageView = findViewById(R.id.wheel_image);
        wv_second = findViewById(R.id.options2);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick(wv_first.getCurrentItem(), wv_second.getCurrentItem());
                }
            }
        });
        setStyle();
        initWheelView();
    }

    public void setSelectedTime(boolean isOpen, String start, String end){
        String[] startHM = start.split(":");
        String[] endHM = end.split(":");
        startH = Integer.valueOf(startHM[0]);
        startM = Integer.valueOf(startHM[1]);
        initWheelView();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
//        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_second.setGravity(Gravity.CENTER);
        wv_second.setCyclic(false);
        wv_second.setAlphaGradient(true);
//        wv_second.setLabelOffset(5);
        wv_second.setItemsVisibleCount(5);

        wv_second.setDividerColor(0x00000000);
        wv_second.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_second.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wheelImageView.setVisibility(isSingle ? GONE : VISIBLE);
        wv_second.setVisibility(isSingle ? GONE : VISIBLE);
    }

    private void initWheelView(){
        if(wv_first == null || wv_second == null ){
            return;
        }
        List<SunriseSunsetTimeBean> items = new ArrayList<>();
        int[] mins = {-120,-60,-30,-10,-5,0,5,10,30,60,120};
        for(int i = 0; i < mins.length * 2;i++){
            SunriseSunsetTimeBean sunriseSunsetTimeBean = new SunriseSunsetTimeBean();
            String timeStr = "";
            int time = 0;
            if(i < 11){
                time =mins[i];
                timeStr = "日出";
            }else{
                time = mins[i-11];
                timeStr = "日落";
            }
            sunriseSunsetTimeBean.setValue(time * 60);
            if(time < 0){
                time = -time;
                timeStr = timeStr + "前";
            }else if(time > 0){
                timeStr = timeStr + "后";
            }
            if(time / 60 != 0){
                timeStr = timeStr + time/60 +"小时";
            }else if(time == 0){
                timeStr = timeStr + "时";
            }else{
                timeStr = timeStr + time +"分钟";
            }
            sunriseSunsetTimeBean.setLabel(timeStr);
            items.add(sunriseSunsetTimeBean);
        }
        mFirstNumericWheelAdapter = new ArrayWheelAdapter(items){
            @Override
            public Object getItem(int index) {
                SunriseSunsetTimeBean sunriseSunsetTimeBean = (SunriseSunsetTimeBean) super.getItem(index);
                return sunriseSunsetTimeBean;
            }
        };
        wv_first.setAdapter(mFirstNumericWheelAdapter);
//        wv_first.setCurrentItem(currentItem);

        mSecondNumericWheelAdapter = new ArrayWheelAdapter(items){
            @Override
            public Object getItem(int index) {
                SunriseSunsetTimeBean sunriseSunsetTimeBean = (SunriseSunsetTimeBean) super.getItem(index);
                return sunriseSunsetTimeBean;
            }
        };
        wv_second.setAdapter(mFirstNumericWheelAdapter);
    }

    public void setSingle(boolean single) {
        isSingle = single;
        if(wheelImageView == null ){
            return;
        }
        wheelImageView.setVisibility(single ? GONE : VISIBLE);
        wv_second.setVisibility(single ? GONE : VISIBLE);
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable BottomSunriseSunsetDialogNew.DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(int startTime, int endTime);
    }
}